﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class T>
struct RBTreeNode
{

	T _data;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;


	RBTreeNode(const T& T)
		:_data(T)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}
};

template<class T>
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T> Self;


	Node* _node;
	RBTreeIterator(Node* node)
		:_node(node)
	{}


	Self& operator++()
	{
		assert(_node);
		Node* cur = _node;
		Node* sonR = _node->_right;
		if (sonR)
		{
			_node = _node->_right;
			return *this;
		}
		Node* parent = _node->_parent;
		while (parent)
		{
			if (cur == parent->_left)
			{
				
				_node = parent;
				return *this;
			}
			else
			{
				cur = parent;
				parent = parent->_parent;
			}
		}
		_node = parent;
		return *this;

	}

	Self operator++(int)
	{
		assert(_node);
		Node* ret = _node;

		Node* cur = _node;
		Node* sonR = _node->_right;
		if (sonR)
		{
			_node = _node->_right;
			return ret;
		}
		Node* parent = _node->_parent;
		while (parent)
		{
			if (cur == parent->_left)
			{

				_node = parent;
				return ret;
			}
			else
			{
				cur = parent;
				parent = parent->_parent;
			}
		}
		_node = parent;
		return ret;

	}

	T& operator*()
	{
		return _node->_data;
	}
	T* operator->()
	{
		return &_node->_data;
	}


	bool operator!=(const Self& s)const
	{
		return _node != s._node;

	}
	bool operator==(const Self& s)const
	{
		return _node == s._node;
	}
};




template<class K, class T, class KeyofT>
class RBTree
{

	typedef RBTreeNode<T> Node;
public:
	
	typedef RBTreeIterator<T>  iterator;

	iterator begin()
	{
		Node* cur = _root;
		while (cur->_left)
		{
			cur = cur->_left;
		}
		return cur;
	}
	iterator end()
	{
		return nullptr;
	}


	~RBTree()
	{
		Destroy(_root);
		_root = nullptr;
	}


	bool Insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}


		KeyofT kot;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}


		cur = new Node(data);

		cur->_col = RED;
		if (kot(parent->_data) < kot(data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
		
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{

					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;


					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
				
					if (cur == parent->_left)
					{
					
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						
						RotateL(parent);
						RotateR(grandfather);


						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else
			{
			
				Node* uncle = grandfather->_left;
			
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					
					cur = grandfather;
					parent = cur->_parent;
				}
				else 
				{
					
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}

		_root->_col = BLACK;
		return true;
	}
private:
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;


		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}

			subR->_parent = parentParent;
		}
	}


	void  RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* parentParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parentParent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subL;
			}
			else
			{
				parentParent->_right = subL;
			}
			subL->_parent = parentParent;
		}
	}

	void Destroy(Node* root)
	{
		if (root == nullptr)
			return;

		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}

private:
	Node* _root = nullptr;
};